/**
 * Check if an element has a class
 * @param {HTMLElement} ele
 * @param {string} cls
 * @returns {boolean}
 */
export function hasClass(ele: HTMLElement, cls: string) {
  return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
}

/**
 * Add class to element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function addClass(ele: HTMLElement, cls: string) {
  if (!hasClass(ele, cls)) ele.className += " " + cls;
}

/**
 * Remove class from element
 * @param {HTMLElement} ele
 * @param {string} cls
 */
export function removeClass(ele: HTMLElement, cls: string) {
  if (hasClass(ele, cls)) {
    const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
    ele.className = ele.className.replace(reg, " ");
  }
}

/**
 * 判断是否是外部链接
 *
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}
// 表格超出显示...
export function tableRowFormatter(data: any) {
  console.log("11111111111111111");
  console.log(data, "data");
  if (typeof data.cellValue == "string") {
    return h("div", { class: "popperClass" }, data.cellValue);
  } else {
    let list = "";
    data.cellValue.forEach((item: any) => {
      list = list + item.labelName + ":" + item.labelValue + ",";
    });
    console.log(list);
    return h("div", { class: "popperClass" }, list);
  }
}
// 表单input正整数软校验
export function validatePositiveInteger(rule: any, value: any, callback: any) {
  if (!value) {
    return callback(new Error("请输入正整数"));
  }
  if (!/^\d+(\.\d{1,2})?$/.test(value)) {
    return callback(new Error("请输入有效的正整数"));
  }
  callback();
}
// 表单input正整数软校验
export function validatePositiveIntegers(rule: any, value: any, callback: any) {
  if (!value) {
    return callback(new Error("请输入正整数"));
  }
  if (!/^(?!0\d)\d*(?:\.\d{0,2})?$/.test(value)) {
    return callback(new Error("只能输入2位小数或正整数"));
  }
  callback();
}
